package com.ffcs.crmd.tsp.admin.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.ctg.itrdc.platform.common.entity.PageInfo;
import com.ctg.itrdc.platform.common.utils.bean.BeanUtils;
import com.ffcs.crmd.tsp.admin.entity.TaskIdentity;
import com.ffcs.crmd.tsp.admin.service.ITaskIdentityService;
import com.ffcs.crmd.tsp.api.dto.client.TspResult;
import com.ffcs.crmd.tsp.api.dto.client.TspTaskIdentity;
import com.ffcs.crmd.tsp.client.core.TspClient;
import com.ffcs.crmd.tsp.common.exception.TspException;

/**
 * 
 * 功能说明:执行端注册信息服务
 *
 * @author FFCS-ZHONGFUHUA
 * 
 * @since 1.2.0-SNAPSHOT
 *
 */
@Service
public class TaskIdentityServiceImpl extends AbstractService implements ITaskIdentityService {
    
    @Override
    public void deleteTaskIdentity(List<TaskIdentity> taskRegisterInfoList) throws TspException {
        for(TaskIdentity taskIdentity : taskRegisterInfoList){
            deleteTaskIdentity(taskIdentity);
        }
    }
    
    @Override
    public void deleteTaskIdentity(TaskIdentity taskIdentity) throws TspException {
        try {
            TspTaskIdentity tspTaskIdentityVO = new TspTaskIdentity();
            tspTaskIdentityVO.setClusterName(taskIdentity.getClusterName());
            tspTaskIdentityVO.setId(taskIdentity.getId());
            
            TspClient tspClient = this.getClient(taskIdentity.getClusterName());
            TspResult<?> tspResult = tspClient.deleteTaskIdentity(tspTaskIdentityVO);
            validateResult(tspResult);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    @Override
    public TaskIdentity get(TaskIdentity taskIdentity) throws TspException {
        try {
            TspTaskIdentity tspTaskIdentityVO = new TspTaskIdentity();
            BeanUtils.applyIf(tspTaskIdentityVO, taskIdentity, false);
            
            TspClient tspClient = this.getClient(taskIdentity.getClusterName());
            TspResult<TspTaskIdentity> tspResult = tspClient.queryTaskIdentity(tspTaskIdentityVO);
            validateResult(tspResult);
            
            TaskIdentity rtaskIdentity = new TaskIdentity();
            BeanUtils.applyIf(rtaskIdentity, tspResult.getRetVo().getObject(), false);
            return rtaskIdentity;
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    @Override
    public List<TaskIdentity> query(TaskIdentity taskIdentity) throws TspException {
        try {
            TspTaskIdentity tspTaskIdentityVO = new TspTaskIdentity();
            BeanUtils.applyIf(tspTaskIdentityVO, taskIdentity, false);
            
            TspClient tspClient = this.getClient(taskIdentity.getClusterName());
            TspResult<TspTaskIdentity> tspResult = tspClient.queryTaskIdentity(tspTaskIdentityVO);
            validateResult(tspResult);
            
            List<TspTaskIdentity> tspTaskIdentityVOList = new ArrayList<TspTaskIdentity>();
            tspTaskIdentityVOList.addAll(tspResult.getRetVo().getDataList());
            return BeanUtils.copyList(tspTaskIdentityVOList, TaskIdentity.class);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    
    @Override
    public PageInfo<TaskIdentity> queryPage(TaskIdentity taskIdentity, int pageNumber, int pageSize) throws TspException {
        try {
            TspTaskIdentity tspTaskIdentityVO = new TspTaskIdentity();
            BeanUtils.applyIf(tspTaskIdentityVO, taskIdentity, false);
            tspTaskIdentityVO.setPageNumber(pageNumber);
            tspTaskIdentityVO.setPageSize(pageSize);
            
            TspClient tspClient = getClient(taskIdentity.getClusterName());
            TspResult<TspTaskIdentity> tspResult = tspClient.queryTaskIdentity(tspTaskIdentityVO);
            validateResult(tspResult);
            
            com.ffcs.crmd.tsp.api.dto.page.PageInfo<TspTaskIdentity> tspJobGroupPageInfo = tspResult.getRetVo().getPageInfo();
            PageInfo<TaskIdentity> jobGroupPageInfo = new PageInfo<TaskIdentity>();
            BeanUtils.applyIf(jobGroupPageInfo, tspJobGroupPageInfo, false);
            jobGroupPageInfo.setList(BeanUtils.copyList(tspJobGroupPageInfo.getList(), TaskIdentity.class));
            return jobGroupPageInfo;
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    
    @Override
    public void saveTaskIdentity(TaskIdentity taskIdentity) throws TspException {
        try {
            TspTaskIdentity tspTaskIdentityVO = new TspTaskIdentity();
            BeanUtils.applyIf(tspTaskIdentityVO, taskIdentity);
            
            TspClient tspClient = this.getClient(taskIdentity.getClusterName());
            TspResult<?> tspResult = tspClient.createTaskIdentity(tspTaskIdentityVO);
            validateResult(tspResult);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }

    
    @Override
    public void updateTaskIdentity(TaskIdentity taskIdentity) throws TspException  {
        try {
            TspTaskIdentity tspTaskIdentityVO = new TspTaskIdentity();
            BeanUtils.applyIf(tspTaskIdentityVO, taskIdentity);
            
            TspClient tspClient = this.getClient(taskIdentity.getClusterName());
            TspResult<?> tspResult = tspClient.updateTaskIdentity(tspTaskIdentityVO);
            validateResult(tspResult);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
}
